package com.example.lab.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.lab.entity.Laboratories;
import com.example.lab.entity.SafetyChecks;
import com.example.lab.entity.Semesters;
import com.example.lab.entity.User;
import com.example.lab.entity.dto.SafetyCheckDTO;
import com.example.lab.mapper.LaboratoriesMapper;
import com.example.lab.mapper.SafetyChecksMapper;
import com.example.lab.mapper.SemestersMapper;
import com.example.lab.mapper.UserMapper;
import com.example.lab.result.PageResult;
import com.example.lab.service.ISafetyChecksService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.lab.service.ISemestersService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 实验室安全检查记录表 服务实现类
 * </p>
 *
 * @author 布莱恩
 * @since 2025-06-17
 */
@Service
@RequiredArgsConstructor
public class SafetyChecksServiceImpl extends ServiceImpl<SafetyChecksMapper, SafetyChecks> implements ISafetyChecksService {

    private final SemestersMapper semestersMapper;
    private final LaboratoriesMapper laboratoriesMapper;
    private final UserMapper userMapper;

    @Override
    public PageResult<SafetyCheckDTO> getSafetyChecks(Integer semesterId, Integer labId, String status,
                                                      LocalDate startDate, LocalDate endDate,
                                                      Integer page, Integer limit) {
        // 1. 构建查询条件
        QueryWrapper<SafetyChecks> queryWrapper = new QueryWrapper<>();
        if (semesterId != null) {
            queryWrapper.eq("semester_id", semesterId);
        }
        if (labId != null) {
            queryWrapper.eq("lab_id", labId);
        }
        if (status != null) {
            queryWrapper.eq("status", status);
        }
        if (startDate != null && endDate != null) {
            queryWrapper.between("check_date", startDate, endDate);
        }

        // 2. 分页查询安全检查记录
        Page<SafetyChecks> pageResult = page(new Page<>(page, limit), queryWrapper);

        // 3. 转换为DTO列表
        List<SafetyCheckDTO> dtoList = pageResult.getRecords().stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());

        // 4. 返回分页结果
        return new PageResult<>(
                dtoList,
                pageResult.getTotal(),
                pageResult.getCurrent(),
                pageResult.getSize()
        );
    }

    private SafetyCheckDTO convertToDTO(SafetyChecks check) {
        SafetyCheckDTO dto = new SafetyCheckDTO();
        BeanUtils.copyProperties(check, dto);

        // 获取学期名称
        Semesters semester = semestersMapper.selectById(check.getSemesterId());
        if (semester != null) {
            dto.setSemesterName(semester.getSemesterName());
        }

        // 获取实验室名称
        Laboratories lab = laboratoriesMapper.selectById(check.getLabId());
        if (lab != null) {
            dto.setLabName(lab.getLabName());
        }

        // 获取检查人姓名
        User user = userMapper.selectById(check.getUserId());
        if (user != null) {
            dto.setUserName(user.getRealName());
        }

        return dto;
    }

    @Override
    public boolean createSafetyCheck(SafetyChecks safetyCheck) {
        return save(safetyCheck);
    }

    @Override
    public boolean reportSafetyCheck(Integer checkId) {
        SafetyChecks safetyCheck = new SafetyChecks();
        safetyCheck.setCheckId(checkId);
        safetyCheck.setStatus("reported");
        safetyCheck.setReportTime(LocalDateTime.now());
        return updateById(safetyCheck);
    }

    @Override
    public boolean processSafetyCheck(Integer checkId, String processDescription) {
        SafetyChecks safetyCheck = new SafetyChecks();
        safetyCheck.setCheckId(checkId);
        safetyCheck.setStatus("processed");
        safetyCheck.setProcessTime(LocalDateTime.now());
        safetyCheck.setProcessDescription(processDescription);
        return updateById(safetyCheck);
    }
}
